package com.totoro.controller;

import com.totoro.pojo.Category;
import com.totoro.service.CategoryService;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author TOTORO
 * @date 2021/5/18 16:03
 * 分类控制器，仅仅用于查询所有分类
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService csi;

    /**
     * 获取所有一级分类
     * @return
     */
    @RequestMapping(value = "/getFirstCategory", method = RequestMethod.POST)
    public String checkFirstCategory(){
        // 获取所有一级分类
        List<Category> categoryList = csi.findOneLevel();
        // 转换Json
        JSONArray jsonArray = JSONArray.fromObject(categoryList);
        return jsonArray.toString();
    }

    /**
     * 获取所有分类
     * @param parentId
     * @param level
     * @return
     */
    @RequestMapping("/getAllCategory")
    public String checkAllCategory(@RequestParam("parent_id") String parentId, @RequestParam("level") int level){
        List<Category> categoryList = null;
        if(level == 2) {
            // 获取所有二级分类
            categoryList = csi.findTwoLevel(parentId);
        }
        if(level == 3){
            // 获取所有三级分类
            categoryList = csi.findThreeLevel(parentId);
        }
        if(level == 4 || level == 5){
            // 获取所有四级分类 或者 五级分类
            categoryList = csi.findFourOrFiveLevel(parentId);
        }
        // 转换Json
        JSONArray jsonArray = JSONArray.fromObject(categoryList);
        return jsonArray.toString();
    }


    /**
     * 根据搜索的关键字查询多数的分类字符
     * @param text
     * @return
     */
    @RequestMapping(value = "/getMoreCategory", method = RequestMethod.POST)
    public String checkMoreCategory(@RequestParam("text") String text){
        String jsonArray = getKeywordPath(text);
        return jsonArray;
    }

    /**
     * 查询数据 根据 类别名字
     * @param name
     * @return
     */
    @RequestMapping("/underName")
    public String underName(@RequestParam("name") String name){
        List<Category> categories = csi.underNameCheck(name);
        JSONArray jsonArray = JSONArray.fromObject(categories);
        return jsonArray.toString();
    }


    /**
     * 获取分类结合json字符串
     * @param text
     * @return
     */
    public String getKeywordPath(String text){
        Map<Integer, Category> map = csi.findKeywordData(text);
        String[] pathStr = new String[map.size()];
        // 初始化数组
        for (int i = 0; i < pathStr.length; i++) {
            pathStr[i]="";
        }
        String path = "";

        for (int i = 0; i < map.size(); i++) {
            // 分割str字符串
            String[] ids = map.get(i).getPath().split(",");
            for (String str2: ids) {
                // 查询该路径id的名字
                String ctName = csi.byIdFindCategoryName(Integer.parseInt(str2));
                pathStr[i]+=ctName+">";
            }
            // 查询本身id的名字
            String selfName = csi.byIdFindCategoryName(map.get(i).getId());
            pathStr[i]+=selfName;
        }

        // 将数组转为List集合进行Json转换
        List<String> arrayList = new ArrayList<>();
        for (String s : pathStr) {
            arrayList.add(s);
        }
        // 转json
        JSONArray jsonArray = JSONArray.fromObject(arrayList);
        return jsonArray.toString();
    }

}
